home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 414_02 / nonport / resizew.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-17  |  3.9 KB  |  144 lines

  1. #ifndef NO_MEMORY_H
  2. #include <memory.h>
  3. #endif
  4. #define    CURSES_LIBRARY    1
  5. #include <curses.h>
  6. #undef    resize_win
  7.  
  8. #ifdef PDCDEBUG
  9. char *rcsid_resizew = "$Header: C:\CURSES\nonport\RCS\resizew.c 2.1 1993/06/18 20:22:16 MH Rel MH $";
  10. #endif
  11.  
  12.  
  13.  
  14.  
  15. /*man-start*********************************************************************
  16.  
  17.   resize_win()    -    Resize a window
  18.  
  19.   PDCurses Description:
  20.      Resizes the passed WINDOW* to reflect the maximum size
  21.      represented by the WINDOW* fields _pmaxy and _pmaxx.
  22.  
  23.      If _maxy < _pmaxy then new lines will be added.
  24.      The same is also true for _maxx and _pmaxx.
  25.  
  26.      WARNING TO PROGRAMMERS:
  27.  
  28.      This function assumes that when a window is enlarged
  29.      horizontally, the contents on each existing line will be
  30.      copied to the new, enlarged line and the remainder of the
  31.      enlarged line will be cleared.
  32.  
  33.      When a window is enlarged vertically, each new line added
  34.      will be blank.
  35.  
  36.      All borders will be observed and enlarged appropriately.
  37.  
  38.      When a windows are shrunk, only the WINDOW* fields _pmaxy and
  39.      _pmaxx are affected.
  40.  
  41.   PDCurses Return Value:
  42.      The resize_win() function returns a NULL pointer or a valid
  43.      WINDOW* which may or may not point to the same physical
  44.      window.  Besure to change all pointers to the passed window
  45.      to the address returned by this function (but only if it is
  46.      non-zero).
  47.  
  48.   PDCurses Errors:
  49.      It is an error to pass a NULL WINDOW pointer.
  50.  
  51.   Portability:
  52.      PDCurses    WINDOW*    resize_win( WINDOW* w, int lins, int cols );
  53.  
  54. **man-end**********************************************************************/
  55.  
  56. WINDOW*    resize_win(WINDOW *w, int lins, int cols)
  57. {
  58. extern    void*    (*mallc)();    /* ptr to some malloc(size)    */
  59. extern    void*    (*callc)();    /* ptr to some ecalloc(num,size)*/
  60. extern    void    (*fre)();    /* ptr to some free(ptr)    */
  61.  
  62.     WINDOW*    new;
  63.     int    ncols  = max(cols, w->_pmaxx);
  64.     int    nlines = max(lins, w->_pmaxy);
  65.     int    i;
  66.     int    j;
  67.  
  68. #ifdef PDCDEBUG
  69.     if (trace_on) PDC_debug("resize_win() - called: lins %d cols %d\n",lins,cols);
  70. #endif
  71.  
  72.     if (w == (WINDOW *)NULL)
  73.         return( (WINDOW *)NULL );
  74.  
  75.     if ((lins > w->_pmaxy) || (cols > w->_pmaxx))
  76.     {
  77.         if ((new = PDC_makenew(nlines, ncols, w->_begy, w->_begx)) == (WINDOW *)NULL)
  78.             return( (WINDOW *)NULL );
  79.  
  80.         new->_curx = w->_curx;
  81.         new->_cury = w->_cury;
  82.         new->_flags = w->_flags;
  83.         new->_attrs = w->_attrs;
  84.         new->_tabsize = w->_tabsize;
  85.         new->_clear = w->_clear;
  86.         new->_leave = w->_leave;
  87.         new->_scroll = w->_scroll;
  88.         new->_nodelay = w->_nodelay;
  89.         new->_use_keypad = w->_use_keypad;
  90.         new->_tmarg = w->_tmarg;
  91.         new->_bmarg = w->_bmarg;
  92.         new->_title = w->_title;
  93.         new->_title_ofs = w->_title_ofs;
  94.         new->_title_attr = w->_title_attr;
  95.         new->_parent = w->_parent;
  96.         memcpy(new->_borderchars, w->_borderchars, 8*sizeof(chtype));
  97.  
  98.         for (i = 0; i < nlines; i++)
  99.         {
  100.             /*
  101.              * make and clear the lines
  102.              */
  103.             if ((new->_y[i] = (chtype*)(*callc)(ncols, sizeof(chtype))) == NULL)
  104.             {
  105.                 for (j = 0; j < i; j++)
  106.                 {
  107.                     /*
  108.                      * if error, free all the data
  109.                      */
  110.                     (*fre)(new->_y[j]);
  111.                 }
  112.                 (*fre)(new->_firstch);
  113.                 (*fre)(new->_lastch);
  114.                 (*fre)(new->_y);
  115.                 (*fre)(new);
  116.                 return( (WINDOW *)NULL );
  117.             }
  118.         }
  119.         if ((w != curscr))
  120.         {
  121.             overwrite(w, new);
  122.             wmove(new, w->_maxy - 1, 0);
  123.             wclrtobot(new);
  124. /* JGB box uses defaults if arguments are zero, but we don't want to do
  125.    this if the window currently has no box */
  126.             if (w->_borderchars[0] || w->_borderchars[2])
  127.                 box(new, w->_borderchars[0], w->_borderchars[2]);
  128.         }
  129.         delwin(w);
  130.         return( new );
  131.     }
  132.     else
  133.     {
  134.         w->_bmarg = lins - 1;
  135.         w->_maxy = lins;
  136.         w->_maxx = cols;
  137. /* JGB box uses defaults if arguments are zero, but we don't want to do
  138.    this if the window currently has no box */
  139.         if (w->_borderchars[0] || w->_borderchars[2])
  140.             box(w, w->_borderchars[0], w->_borderchars[2]);
  141.         return( w );
  142.     }
  143. }
  144.